package io;

import java.io.*;
import java.net.URL;

/**
 * @Author:zimmor
 * @Date: 2021/11/16 15:04
 * @Description
 * @version: 1.0.0
 */

public class IODemo {
    public static void main(String[] args) throws Exception {
        IODemo ioDemo = new IODemo();
        ioDemo.fun2();


    }

    /**
     * 字节流转字符流使用
     * @throws Exception
     */
    public void fun1() throws Exception {
        InputStreamReader re = new InputStreamReader(Object.class.getResource("/c.properties").openStream());
        BufferedReader bufferedReader = new BufferedReader(re);
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
    }

    /**
     * 字符流转字节流使用
     */
    public void fun2() throws Exception {

        //创建一个文件
        File file = new File("D:\\work\\projectSet2\\myProject\\javaDemo\\src\\main\\resources\\b.txt");
        boolean flag = false;
        if(!file.exists()) {
            flag = file.createNewFile();
        } else {
            file.delete();
            flag = file.createNewFile();
        }
       //把c.properties中的内容写到b.txt中
        //获取输入流
        InputStream inputStream = Object.class.getResourceAsStream("/c.properties");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        //获取输出流
        OutputStreamWriter w = new OutputStreamWriter(new FileOutputStream(file));
        //写入内容
        String line = null;
        while((line = bufferedReader.readLine()) != null) {
            w.write(line);
            w.write('\n');
        }
        inputStream.close();
        bufferedReader.close();
        w.close();
    }

}
